package com.csweimei.rentmanagementtreasure.enterprise.viewModel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.http.entity.DealHouseListBean;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class DealHouseVM extends TitleVM {
    public DealHouseVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("成交房源");
    }

    public ObservableField<String> companyid = new ObservableField<>();
    public ObservableField<String> keyword = new ObservableField<>();
    public ObservableField<String> staTime = new ObservableField<>();
    public ObservableField<String> endTime = new ObservableField<>();

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }


    public ObservableList<DealHouseItemPVM> observableList = new ObservableArrayList<>();
    public ItemBinding<DealHouseItemPVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_deal_house_p);
    public ObservableField<Integer> showNo = new ObservableField<>(View.VISIBLE);


    private void getData() {
        Map<String,String> map = new HashMap<>();
        map.put("companyid",companyid.get());
        map.put("keyword",keyword.get());
        map.put("staTime",staTime.get());
        map.put("endTime",endTime.get());
        model.deallist(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<DealHouseListBean>() {
                    @Override
                    public void onNext(DealHouseListBean o) {
                        if (o.issucc){
                            observableList.clear();
                            if (o.getData().size() > 0){

                                if (ValidateUtils.isEmptyString(o.getData().get(0).getNickname())){
                                    showNo.set(View.VISIBLE);
                                }else {
                                    showNo.set(View.GONE);
                                    for (DealHouseListBean.DataDTO bean : o.getData()) {
                                        DealHouseItemPVM dealHouseItemPVM = new DealHouseItemPVM(titleVM);
                                        dealHouseItemPVM.setData(bean);
                                        observableList.add(dealHouseItemPVM);
                                    }
                                }


                            }else {
                                showNo.set(View.VISIBLE);
                            }
                        }else {
                            showNo.set(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        showNo.set(View.VISIBLE);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}

